const {
  GiftShopModel
} = require('../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    provinceCode: {
      type: String,
      value: ''
    },
    cityCode: {
      type: String,
      value: ''
    },
    districtCode: {
      type: String,
      value: ''
    },
    streetCode: {
      type: String,
      value: ''
    },
    cityShow: {
      type: Boolean,
      value: false,
      observer: function (newVal, oldVal) {
        if (newVal) {
          this.getCityData()
          this.getAddressData()
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    provinceName: '', //省
    provinceCode: '', //省
    cityName: '', //省
    cityCode: '', //省
    districtName: '', //区
    districtCode: '', //区
    streetName: '', //街道
    streetCode: '', //街道
    moduleShow: null, //1、省，2、市，3、区，4、街道
    provincelist: [], //省
    cityList: [], //市
    districtList: [], //区
    streetList: [], //街道
    anchorList: [], //右侧锚点
    jumpNum: '', //跳转到那个字母
    myRegion: {}, //向父级传递参数
    areaHot: [] //热门城市
  },

  /**
   * 组件的方法列表
   */
  methods: {
    resetData(newVal) {
      this.getIndexCharacter(newVal)
      this.setData({
        provincelist: newVal
      })
    },
    _filter(data, code) {
      let result = [];
      if (data && data.length > 0) {
        for (let i = 0; i < data.length; i++) {
          for (let j = 0; j < data[i].address.length; j++) {
            if (code == data[i].address[j].addressCode) {
              result = data[i].address[j];
            }
          }
        }
      }
      return result;
    },
    /**
     * 获取省市区地址
     */
    getAddressData: function () {
       let that = this;
      giftShopModel.areaLocalTotalInfo({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        that.setData({
          provincelist: data || []
        }, function () {
          that.filterData()
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },

    /**
     * 热门城市
     */
    getCityData: function () {
      giftShopModel.areaHot({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        this.setData({
          areaHot: data || []
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },
    /**
     * 选择热门城市
     */
    hotCity(e) {
      let detail = e.currentTarget.dataset.detail;
      if(this.data.provinceCode!=detail.provinceCode){
        this.setData({
          cityName: '', //市
          cityCode: '', //市
          cityList: [], //市
          districtName: '', //区
          districtCode: '', //区
          districtList: [], //区
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      let selectProvince = this._filter(this.data.provincelist, detail.provinceCode);
      this.setData({
        provinceName: detail.provinceName,
        provinceCode: detail.provinceCode,
        cityList: selectProvince.child || [], //区
        moduleShow: 2,
      })
      if (detail.provinceCode != detail.addressCode) {
        let selectCity = this._filter(selectProvince.child, detail.addressCode);
        this.setData({
          cityName: detail.addressName, //市
          cityCode: detail.addressCode, //市
          districtList: selectCity.child || [], //区
          moduleShow: 3,
        })
        this.getIndexCharacter(selectCity.child)
      }else{
        this.getIndexCharacter(selectProvince.child)
      }
    },
    /**
     * 已选择省市区街道
     */
    filterData() {
      let that=this;
      if (that.data.provinceCode != '') {
        let selectProvince = that._filter(that.data.provincelist, that.data.provinceCode);
        that.setData({
          provinceName: selectProvince.addressName || '', //省
          provinceCode: selectProvince.addressCode || '', //省
          cityList: selectProvince.child || [], //市
        })
        let selectCity = that._filter(that.data.cityList, that.data.cityCode);
        that.setData({
          cityName: selectCity.addressName || '', //市
          cityCode: selectCity.addressCode || '', //市
          districtList: selectCity.child || [], //区
        })
        let selectArea = that._filter(that.data.districtList, that.data.districtCode);
        that.setData({
          districtName: selectArea.addressName || '', //区
          districtCode: selectArea.addressCode || '', //区
          streetList: selectArea.child || [], //街道
        })
        let selectStreet = that._filter(that.data.streetList, that.data.streetCode);
        that.setData({
          streetName: selectStreet.addressName || '', //街道
          streetCode: selectStreet.addressCode || '', //街道
        })
        let _val = ''
        if (that.data.streetCode && selectArea.child && selectArea.child.length > 0) {
          _val = 4
          that.getIndexCharacter(selectArea.child)
        } else {
          if (selectCity.child && selectCity.child.length > 0) {
            _val = 3
            that.getIndexCharacter(selectCity.child)
          }
        }
        var myRegion = {
          provinceName: that.data.provinceName, //省
          provinceCode: that.data.provinceCode, //省
          cityName: that.data.cityName, //市
          cityCode: that.data.cityCode, //市
          districtName: that.data.districtName, //区
          districtCode: that.data.districtCode, //区
          streetName: that.data.streetName, //街道
          streetCode: that.data.streetCode, //街道
        }
        that.setData({
          myRegion: myRegion,
          moduleShow: _val
        })
      }else{
        that.setData({
          moduleShow: 1
        })
        that.getIndexCharacter(that.data.provincelist)
      }
    },

    /**
     * 省
     * @param {*} e 
     */
    selectProvince(e) {
      let detail = e.currentTarget.dataset.detail;
      if (this.data.provinceCode !== detail.addressCode) {
        this.setData({
          cityName: '', //市
          cityCode: '', //市
          cityList: [], //市
          districtName: '', //区
          districtCode: '', //区
          districtList: [], //区
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      this.getIndexCharacter(detail.child)
      // 点击选择当前
      this.setData({
        cityList: detail.child, //城市
        moduleShow: 2,
        provinceName: detail.addressName, //省
        provinceCode: detail.addressCode, //省
      })
    },
    /**
     * 市
     * @param {*} e 
     */
    selectCity(e) {
      let detail = e.currentTarget.dataset.detail;
      if (this.data.cityCode !== detail.addressCode) {
        this.setData({
          districtName: '', //区
          districtCode: '', //区
          districtList: [], //区
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      this.getIndexCharacter(detail.child)
      // 点击选择当前
      this.setData({
        districtList: detail.child, //区域
        moduleShow: 3,
        cityName: detail.addressName, //省
        cityCode: detail.addressCode, //省
      })
    },
    /**
     * 区
     * @param {*} e 
     */
    selectArea(e) {
      let detail = e.currentTarget.dataset.detail;
      if (this.data.districtCode !== detail.addressCode) {
        this.setData({
          streetName: '', //街道
          streetCode: '', //街道
          streetList: [], //街道
          anchorList: [], //右侧锚点
        })
      }
      // 点击选择当前
      this.setData({
        districtName: detail.addressName, //街道
        districtCode: detail.addressCode, //街道
      })
      if (detail.child && detail.child.length > 0) {
        this.getIndexCharacter(detail.child)
        this.setData({
          streetList: detail.child,
          moduleShow: 4
        })
      } else {
        this.selectRegionData();
      }

    },
    /**
     * 街道
     * @param {*} e 
     */
    selectStreet(e) {
      let detail = e.currentTarget.dataset.detail;
      // 点击选择当前
      this.setData({
        streetName: detail.addressName, //街道
        streetCode: detail.addressCode, //街道
      })
      this.selectRegionData();
    },

    /**
     * 右侧锚点
     */
    getIndexCharacter(data) {
      const anchorData = (data || []).map(item => {
        return {
          id: item.indexCharacter,
        }
      });
      this.setData({
        anchorList: anchorData,
      })
    },
    // 右侧字母点击事件
    jumpMt(e) {
      let jumpNum = e.currentTarget.dataset.id;
      this.setData({
        jumpNum: jumpNum
      });
    },
    /**
     * 选择省市区街道
     */
    getSelectId: function (e) {
      let id = e.currentTarget.dataset.id;
      this.setData({
        moduleShow: id
      })
      let dataList = []
      if (id == 1) {
        dataList = this.data.provincelist
      } else if (id == 2) {
        dataList = this.data.cityList
      } else if (id == 3) {
        dataList = this.data.districtList
      } else if (id == 4) {
        dataList = this.data.streetList
      }
      this.getIndexCharacter(dataList)
    },

    /**
     * 选择数据
     */
    selectRegionData() {
      var myRegion = {
        provinceName: this.data.provinceName, //省
        provinceCode: this.data.provinceCode, //省
        cityName: this.data.cityName, //市
        cityCode: this.data.cityCode, //市
        districtName: this.data.districtName, //区
        districtCode: this.data.districtCode, //区
        streetName: this.data.streetName, //街道
        streetCode: this.data.streetCode, //街道
      }
      let myEventOption = {
        bubbles: false, //事件是否冒泡
        composed: false, //事件是否可以穿越组件边界
        capturePhase: false //事件是否拥有捕获阶段
      } // 触发事件的选项
      this.setData({
        myRegion: myRegion,
        cityShow: false, //弹出层
      })
      this.triggerEvent('detail', myRegion, myEventOption);
    },

    /**
     * 关闭弹出层
     */
    closeLayer() {
      let that=this;
      that.setData({
        provinceName: '', //省
        provinceCode: '', //省
        cityName: '', //省
        cityCode: '', //省
        districtName: '', //区
        districtCode: '', //区
        streetName: '', //街道
        streetCode: '', //街道
        provinceList: [], //省
        cityList: [], //市
        districtList: [], //区
        streetList: [], //街道
        anchorList: [], //右侧锚点
        cityShow: false, //弹出层
        moduleShow: null,
      },function(){
        let myEventOption = {
          bubbles: false, //事件是否冒泡
          composed: false, //事件是否可以穿越组件边界
          capturePhase: false //事件是否拥有捕获阶段
        } // 触发事件的选项
        this.triggerEvent('detail', that.data.myRegion, myEventOption);
      })
    },

    /**
     * 父级传参
     */
    parentData(myRegion) {
      let myEventOption = {
        bubbles: false, //事件是否冒泡
        composed: false, //事件是否可以穿越组件边界
        capturePhase: false //事件是否拥有捕获阶段
      } // 触发事件的选项
      this.setData({
        myRegion: myRegion,
        cityShow: false, //弹出层
      })
      this.triggerEvent('detail', myRegion, myEventOption);
    }
  }
})